package sg.edu.nus.iss.jass.gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.util.List;

import sg.edu.nus.iss.jass.constant.ActionBtnContants;
import sg.edu.nus.iss.jass.controller.JobSearchController;
import sg.edu.nus.iss.jass.controller.JobSearchController.JobAdFunction;
import sg.edu.nus.iss.jass.controller.MainController;
import sg.edu.nus.iss.jass.dto.JobAdvert;

public class JobSearchFormPanel extends FormPanel {
	
	private SearchJobPanel searchJobPanel;
	private SearchJobResultPanel searchJobResultPanel;
	private JobSearchController jobSearchController;
	
	public JobSearchFormPanel(MainFrame mainframe, ActionPanel actionPanel,
			HeaderPanel headerPanel) {
		super(mainframe, actionPanel, headerPanel);
		jobSearchController = MainController.getInstance().getJobSearchController();
	}

	@Override
	public void show() {
		this.setLayout(new BorderLayout());
		searchJobPanel = new SearchJobPanel(this);
		searchJobResultPanel = new SearchJobResultPanel(this);
		
		this.add(searchJobPanel, BorderLayout.NORTH);
		this.add(searchJobResultPanel, BorderLayout.CENTER);
		
		this.addFormPanel();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equalsIgnoreCase(ActionBtnContants.DELETE)){
			try {
				this.jobSearchController.processJobAd(this.searchJobResultPanel.getSelectedJob(), JobAdFunction.delete);
			} catch (Exception e1) {
				this.showMsg(e1.getMessage(), false);
			}
		}
		else if(e.getActionCommand().equalsIgnoreCase(ActionBtnContants.SELECT_ALL)){
			this.searchJobResultPanel.selectJobAd(true);
		}
		else if(e.getActionCommand().equalsIgnoreCase(ActionBtnContants.DESELECT_ALL)){
			this.searchJobResultPanel.selectJobAd(false);
		}
	}
	
	public void clearSearchResult(){
		this.searchJobResultPanel.clearTable();
	}
	
	public void showSearchResult(List<JobAdvert> jobAdverts){
		this.searchJobResultPanel.refreshSearchResult(jobAdverts);
	}
	
}
